function validateEmail() {
    var email = $('#email').val()
    if (!email) {
        alert('请输入邮箱')
        return false
    }
    var emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
    if (!emailReg.test(email)) {
        alert('请输入正确的邮箱')
        return false
    }
    return true
}

function bindEmailCaptcha() {
    $('#captcha-btn').click(function (event) {
        event.preventDefault()
        var email = $('#email').val()
        if (!email) {
            alert('请输入邮箱')
            return
        }
        try {
            $.ajax({
                url: '/auth/captcha/email?email=' + email,
                method: 'POST',
                data: {
                    email: email
                },
                success: function (data) {
                    var code = data.code
                    if (code == 200) {
                        var countdown = 60
                        $('#captcha-btn').attr('disabled', 'disabled')
                        var interval = setInterval(function () {
                            countdown -= 1
                            $('#captcha-btn').text(countdown + '秒后重试')
                            if (countdown == 0) {
                                clearInterval(interval)
                                $('#captcha-btn').removeAttr('disabled')
                                $('#captcha-btn').text('获取验证码')
                            }
                        }, 1000)
                    } else {
                        alert('验证码发送失败')
                    }
                },
                fail: function (data) {
                    alert(data.message)
                }
            })
        } catch (e) {
            alert('验证码发送失败', e)
        }

    });
}

$(function () {
    bindEmailCaptcha()
})
